import { AvatarEntity } from "db://assets/script/game/demo/ecs/entity/AvatarEntity";
import { MoveComponent } from "db://assets/script/game/demo/ecs/comp/MoveComponent";
import { TransformComponent } from "db://assets/script/game/demo/ecs/comp/TransformComponent";
import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { lerp, macro } from "cc";

export class MoveSystem extends ecs.ComblockSystem<AvatarEntity> implements ecs.IEntityEnterSystem, ecs.ISystemUpdate {

    init() {

    }

    entityEnter(entity: AvatarEntity): void {
        entity.moveComponent.speed = 100;
    }

    filter(): ecs.IMatcher {
        return ecs.allOf(MoveComponent, TransformComponent);
    }

    update(entity: AvatarEntity): void {
        let moveComp = entity.moveComponent;
        let position = entity.transformComponent.position;

        position.x += moveComp.heading.x * moveComp.speed * this.dt;
        position.y += moveComp.heading.y * moveComp.speed * this.dt;

        entity.transformComponent.angle = lerp(entity.transformComponent.angle, Math.atan2(moveComp.heading.y, moveComp.heading.x) * macro.DEG, this.dt)
    }


}